
package br.unirio.bsi.mono.ForumSite.beans.util;

import java.util.Iterator;

import org.apache.struts.action.ActionForm;
import org.apache.struts.validator.DynaValidatorForm;
import org.apache.commons.validator.ValidatorResults;

public class copyBean
{   
  public static void fromActionForm( Object o, ActionForm form )
  {
    DynaValidatorForm dvf = (DynaValidatorForm)form;
    ValidatorResults vr = dvf.getValidatorResults();

    // pega nome dos campos do formulario    
    for( Iterator propNames = vr.getPropertyNames().iterator(); propNames.hasNext(); )
    {      
      String prop = (String)propNames.next();
      Object propval = dvf.get(prop); 
      
      for( Class obj = o.getClass(); !obj.equals(Object.class); obj = obj.getSuperclass() )
      {
        try {
          obj.getDeclaredMethod( "set"+prop, new Class[]{propval.getClass()} ).invoke( o, propval );                  
        } catch( Exception e ){}      
      }      
    }
  }  
}

